package com.llkj.qingnianguoxue.adapter;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.BookingCourseBean;
import com.llkj.qingnianguoxue.util.DateUtils;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 预约公告适配器
 */
public class BookingCourseAdapter extends BaseQuickAdapter<BookingCourseBean, BaseViewHolder> {

    private String mCurrentDate;
    public BookingCourseAdapter(int layoutResId, List<BookingCourseBean> data) {
        super(layoutResId, data);
    }

    public void setCurrentDate(String currentDate){
        mCurrentDate = currentDate;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, BookingCourseBean item) {
        baseViewHolder.setText(R.id.tv_title, item.getName());
        String time = "";
        try {
            if(item.getStart_time().length()>=16){
                time = item.getStart_time().substring(11,16);
            }else{
                time = item.getStart_time();
            }
        }catch (Exception e){}
        try {
            if(item.getEnd_time().length()>=16){
                time += "-"+item.getEnd_time().substring(11,16);
            }else{
                time += "-"+item.getEnd_time();
            }
        }catch (Exception e){}

        baseViewHolder.setText(R.id.tv_time_period, time);
        baseViewHolder.setText(R.id.tv_price, String.format(mContext.getString(R.string.book_course_price),item.getPrice()));

        TextView tvState = baseViewHolder.getView(R.id.tv_state);

        if(!StringUtils.isEmpty(item.getIs_reserve())){
            if("3".equals(item.getIs_reserve())){
                Date date = null;
                //判断处理已过期的课程
                if(item.getStart_time().length()>=16){
                    date = DateUtils.dateFormat(item.getStart_time());
                }else{
                    String datetime = mCurrentDate +" "+item.getStart_time()+":00";
                    date = DateUtils.dateFormat(datetime);
                }

                if(date!=null && System.currentTimeMillis()>date.getTime()){
                    tvState.setSelected(true);
                }else{
                    tvState.setSelected(false);
                }

                tvState.setText("预约");
            }else if("1".equals(item.getIs_reserve())){
                tvState.setSelected(true);
                tvState.setText("已预约");
            }else{
                tvState.setSelected(true);
                tvState.setText("满额");
            }
        }else{
            if("1".equals(item.getIs_yu())){
                tvState.setSelected(false);
                tvState.setText("预约");
            }else{
                tvState.setSelected(true);
            }
        }

    }

}
